package com.imessage.action.api.virtual;

import com.bean.*;
import com.imessage.jackson.JsonObject;
import com.imessage.type.*;
import com.imessage.utils.MessageUtils;
import io.ebean.Expr;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/task/virtual")
public class ApiVirtualAction extends ApiVirtualBaseAction {

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Object get(
            @RequestParam("virtual_mac") String virtualMac) {
        JsonObject dataObject = new JsonObject();
        virtualMac=virtualMac.replaceAll(":", "").toUpperCase();
        DeviceVirtual deviceVirtual = baseService.findObject(
                DeviceVirtual.class, Expr.eq("virtualMac", virtualMac),"","id desc");
        if (deviceVirtual==null){
            return MessageUtils.getErrorMessage("data error");
        }
        BusinessTaskDetail businessTaskDetail=getRunBusinessTaskDetail(deviceVirtual);
        if(businessTaskDetail==null){
            return MessageUtils.getErrorMessage("no task");
        }
        initGetObject(dataObject,businessTaskDetail);
        return MessageUtils.getSuccessMessage(dataObject);
    }


    /**
     * 虚拟机定时反馈当前任务进度
     *
     * @param taskId
     * @param dealNum
     * @return
     */
    @RequestMapping(value = "feedback", method = RequestMethod.GET)
    public Object feedback(@RequestParam("task_id") long taskId,
                           @RequestParam("deal_num") int dealNum) {
        JsonObject dataObject = new JsonObject();
        BusinessTaskDetail businessTaskDetail=baseService.findById(BusinessTaskDetail.class, taskId);
        if(businessTaskDetail==null){
            return MessageUtils.getErrorMessage("data error");
        }
        taskService.updateRunBusinessTaskDetail(businessTaskDetail,dealNum);
        return MessageUtils.getSuccessMessage(dataObject);
    }


    /**
     * 整个任务流程执行完以后报告最终结果
     *
     * @param taskId
     * @param code
     * @param msg
     * @return
     */
    @RequestMapping(value = "result", method = RequestMethod.GET)
    public Object result(@RequestParam("task_id") long taskId,
                         @RequestParam("code") String code,
                         @RequestParam("msg") String msg) {
        JsonObject dataObject = new JsonObject();
        if(code.equals("0")){
            return MessageUtils.getSuccessMessage(dataObject);
        }
        BusinessTaskDetail businessTaskDetail=baseService.findById(BusinessTaskDetail.class, taskId);
        if(businessTaskDetail==null){
            return MessageUtils.getErrorMessage("data error");
        }
        taskService.updateFinishBusinessTaskDetail(businessTaskDetail,msg);
        return MessageUtils.getSuccessMessage(dataObject);
    }



}
